//
//  LDBaseListTextField.swift
//  DingDongZP
//
//  Created by 三秒文化 on 2016/10/18.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit


let titleW:CGFloat = 100

class LDBaseListTextField: LDBaseView {

    
    private let originY:CGFloat = 0
    var isShowMarkImg = false {
        
        didSet{
            
        }
    }
    
    var isShowLine = false {
        
        didSet{
            
        }
    }
    
    var isCanEdit = false {
        didSet{
            
            configureOfTextField(isCanEdit)
        }
    }
    
    private var type:DDInvoiceDetailViewType?
    
    

    //默认title
    private var listTitleArr = ["dfd","dsag","gd","dgds","dgd","dgds"]
    
    //描述数据
    var descTitleArr = [""] {
        
        didSet{
            
            updateUI()
        }
    }
    

    private let titleFieldW:CGFloat = LDFrame.ScreenWidth - titleW
    var descLabArr = [UITextField]()
    private var bottomLineArr = [UIView]()
    
    private var listH:CGFloat = 40
    
    
    private override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    init(listTitleArr:[String],type:DDInvoiceDetailViewType) {
        
        super.init(frame: LDFrame.ScreenBounds)
        
        self.type = type
        self.listTitleArr = listTitleArr
        initUI()
    }
    
    func updateUI() {
        
        var i = 0
    
        for item in descLabArr {
            
            item.text = descTitleArr[i]
            i += 1
        }
    }
    
    func initUI() {
        
        var Y:CGFloat = originY
        for i in 0..<listTitleArr.count {
            
            let frameLab = CGRectMake(leftMargin, Y, titleW, listH)
            let frameField = CGRectMake(LDMargin_V + titleW, Y, titleFieldW, listH)
            
            
            var isLine = false
            
            if type == .Line {
                
                isLine = true
            } else if type == .Edit {
                
                isLine = true
            }
            
            let textField = creatLabAndTextField(listTitleArr[i], desc: listTitleArr[i], frameLab: frameLab, frameField: frameField,isLine: isLine)
            
            Y += listH
            descLabArr.append(textField)
        }
    }
    
    func getH() ->CGFloat {
        
        return CGFloat(listTitleArr.count) * listH
    }

    internal required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //
    func creatLabAndTextField(title:String,desc:String,frameLab:CGRect,frameField:CGRect,isLine:Bool) -> UITextField {
        
        let lab = UILabel.setDetailsLab(title, fontSize: 14, titleColor: LDFontColor.Black, Alignment: .Left)
        let labField = UITextField.init()
        labField.font = LDFont.Font14
        labField.textColor = LDFontColor.LightGray
        
        let bottomLine = UIView()
        bottomLine.backgroundColor = LDLineColorLightGray
        
        lab.frame = frameLab
        labField.frame = frameField
        
        addSubview(lab)
        addSubview(labField)
        
        if isLine == true {
            
            bottomLine.frame = CGRectMake(0, frameLab.origin.y + frameLab.size.height - 0.5, LDFrame.ScreenWidth, 0.5)
            addSubview(bottomLine)
            bottomLineArr.append(bottomLine)
        }
        
        
        return labField
    }
    
    //根据type确定类型
    func configureOfTextField(isEdit:Bool) {
        
        if isEdit == true {
            
            for item in descLabArr {
                
                item.enabled = false
            }
            
        } else  {
            
            for item in descLabArr {
                
                item.enabled = false
            }
        }
    }
    
    
    func cleanViews() {
        
        for item in subviews {
            
            item.removeFromSuperview()
        }
    }

}
